﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Snake {
    public class SnakeManager : MonoBehaviour {
        [SerializeField]
        private Transform[] listSnakeSelection;
        [SerializeField]
        private float intervalDistance = 0.05f;
        
        private GameSnake gameSnake;

        private Vector2 preTouchPosition = Vector2.zero;

        void Start() {
            InitSnake();
        }

        void Update() {
            //TestInput();

            Vector3 touchPosition;
            bool isTouch = OnTouchMove(out touchPosition);
            if (isTouch) {                
                //  fix 长触时蛇会在触点徘徊
                if (Vector2.Distance(preTouchPosition, touchPosition) > 10) {
                    preTouchPosition = touchPosition;

                    Vector3 worldPosition = GetTouchPosition(touchPosition, Camera.main);
                    UpdateMoveTowards(worldPosition);
                }
            }

            // 点击加速
            gameSnake.getSnake().setStepSpeed((Input.GetMouseButton(0) ? 3 : 1) * 0.016f);

            gameSnake.getSnake().Step();
        }

        private bool OnTouchMove(out Vector3 touchPosition) {
            touchPosition = Vector3.zero;

            if (Application.platform == RuntimePlatform.WindowsEditor ||
                Application.platform == RuntimePlatform.OSXEditor ||
                Application.platform == RuntimePlatform.WindowsPlayer ||
                Application.platform == RuntimePlatform.OSXPlayer) {
                if (Input.GetMouseButton(0)) {
                    touchPosition = Input.mousePosition;
                    return true;
                }
            }

            if (Application.platform == RuntimePlatform.IPhonePlayer ||
                Application.platform == RuntimePlatform.Android) {
                if (Input.touchCount > 0) {
                    if (Input.GetTouch(0).phase == TouchPhase.Moved ||
                        Input.GetTouch(0).phase == TouchPhase.Stationary) {
                        touchPosition = Input.GetTouch(0).position;
                        return true;
                    }
                }
            }

            return false;
        }

        private Vector3 GetTouchPosition(Vector3 touchPosition, Camera camera) {
            camera = camera != null ? camera : Camera.main;
            if (camera == null) {
                return touchPosition;
            }
            return camera.ScreenToWorldPoint(touchPosition);
        }

        private void UpdateMoveTowards(Vector3 position) {
            Transform head = gameSnake.getSnake().getHeader().getBody();
            position = head.parent.InverseTransformPoint(position);
            position.z = head.localPosition.z;
            gameSnake.getSnake().setStepDirection((position - head.localPosition).normalized);        
        }

        //private void TestInput() {
        //    if (Input.GetKeyDown(KeyCode.A)) {
        //        for (int i = 0; i < 10; ++i) gameSnake.getSnake().AddOneLength(0.05f);
        //    }

        //    if (Input.GetKeyDown(KeyCode.P)) {
        //        Debug.Log("-----------");
        //        foreach (Segment e in snake) {
        //            if (e.getMinDistanceOfFront() != intervalDistance)
        //                Debug.Log("哎呀,有错误的段:" + e.getMinDistanceOfFront());
        //        }
        //    }
        //}

        private void InitSnake() {
            List<Transform> list = new List<Transform>();
            list.Add(listSnakeSelection[0]);
            Transform mold = listSnakeSelection[1];
            for (int i = 0; i < 10; ++i) {
                Transform t = Instantiate(mold) as Transform;
                t.parent = mold.parent;
                list.Add(t);
            }
            list.Add(listSnakeSelection[2]);
            Snake snake = new Snake(list.ToArray(), mold, intervalDistance);
            gameSnake = new GameSnake(snake);
        }

        internal GameSnake getGameSnake() {
            return gameSnake;
        }
    }
}